package org.example;

import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class FilesFinder {
    private Path root;
    private String regexp;

    private List<Path> fileList;

    public FilesFinder(String root, String regexp) throws IOException {
        walk(Paths.get(root), regexp);
    }

    private void walk(Path root, String regexp) throws IOException {
        this.root = root;
        this.regexp = regexp;

        fileList = Files.walk(root, FileVisitOption.FOLLOW_LINKS)
                .filter(Files::isRegularFile)
                .filter(f -> f.getFileName().toString().matches(regexp))
                .collect(Collectors.toList());
    }

    public Path getRoot() {
        return root;
    }

    public String getRegexp() {
        return regexp;
    }

    public Collection<Path> getFileList() {
        return new ArrayList<>(fileList);
    }

    public String toString() {
        StringJoiner stringJoiner = new StringJoiner(System.lineSeparator());

        for (Path path : getFileList()) {
            stringJoiner.add(path.toString());
        }

        return stringJoiner.toString();
    }
}
